/*
 * SampleApplication.h
 *
 *  Created on: 2010-12-8
 *      Author: try
 */

#ifndef SAMPLEAPPLICATION_H_
#define SAMPLEAPPLICATION_H_
#include <windows.h>
#include "../draw/draw.h"

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define KeyState(key) ((key) & 0x80)

class SampleApplication {
  public:
    SampleApplication(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
    virtual ~SampleApplication();

    /**
     * 在子类中重写此方法来进行初始化工作
     */
    virtual void init(HWND hwnd){}

    /**
     * 在main中调用此方法进入消息循环
     */
    void run();

    /**
     * 返回按键状态序列
     */
    Ed::KeySequence& getKeySequence(Ed::KeySequence& ks){
      if(GetKeyboardState(keys)){
        for(int i=0; i<256; i++){
          if(KeyState(keys[i])){
            ks.add(i);
          }
        }
      }
      return ks;
    }
  protected:
    /**
     * 在子类中重写此方法时需要回调此方法
     */
    virtual LRESULT wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

  protected:
    virtual void onPaint(){};
    virtual void onMouseMove(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseLeftPress(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseLeftRelease(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseLeftDoubleClick(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseRightPress(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseRightRelease(int x, int y, unsigned int buttonsStatus){}
    virtual void onMouseWheelEvent(int x, int y, int zDelta, unsigned int buttonsStatus){}
    virtual bool onSetCursor(){return false;}
    virtual void onKeyPress(int key){}
    virtual void onKeyRelease(int key){}

  protected:
    HINSTANCE hInstance;
    HINSTANCE hPrevInstance;
    LPSTR lpCmdLine;
    int nCmdShow;
    HWND hwnd;
  private:
    static LRESULT CALLBACK _WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    static SampleApplication *appn;
  private:
    unsigned char keys[256];
};

#endif /* SAMPLEAPPLICATION_H_ */
